/*
 * Copyright (c) 2017. 深圳一七科技. All rights reserved.
 */

package com.yiqi.liebang.feature.mine.presenter;

import android.text.TextUtils;

import com.suozhang.framework.utils.ValidatorUtil;
import com.yiqi.liebang.common.util.RxRegTool;
import com.yiqi.liebang.entity.bo.OrganizationBo;
import com.yiqi.liebang.feature.mine.contract.EnterpriseContract;
import com.yiqi.liebang.feature.mine.dependencies.enterprise.DaggerEnterprisePresenterComponent;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2018/7/11 9:31
 */
public class EnterprisePresenter implements EnterpriseContract.Presenter {

    private EnterpriseContract.View mView;

    @Inject
    EnterpriseContract.Model mModel;


    public EnterprisePresenter(EnterpriseContract.View mView) {
        this.mView = mView;
        DaggerEnterprisePresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void getorganizationCardData() {
        mModel.getorganizationCardData().compose(mView.bindToLife()).subscribe(new Observer<OrganizationBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(OrganizationBo organizationBo) {
                mView.showOrganizationInfoSuccess(organizationBo);
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void organizationCertification(String organizationFullName, String organizationName, String contactName,
                                          String contactPosition, String contactMobile, String contactEmail,
                                          String contactCity, String contactAddress, String businessLicense) {
        if (TextUtils.isEmpty(organizationName)) {
            mView.showError("机构名称不能为空！");
            return;
        }
        if (TextUtils.isEmpty(organizationFullName)) {
            mView.showError("机构全称不能为空！");
            return;
        }
        if (TextUtils.isEmpty(contactName)) {
            mView.showError("联系人姓名不能为空！");
            return;
        }
        if (TextUtils.isEmpty(contactPosition)) {
            mView.showError("联系人职位不能为空！");
            return;
        }
        if (TextUtils.isEmpty(contactMobile)) {
            mView.showError("联系人手机号不能为空！");
            return;
        }
        if (!ValidatorUtil.isMobile(contactMobile)) {
            mView.showError("手机号格式不正确！");
            return;
        }
        if (TextUtils.isEmpty(contactEmail)) {
            mView.showError("邮箱不能为空！");
            return;
        }
        if (!RxRegTool.isEmail(contactEmail)) {
            mView.showError("请输入正确的邮箱！！");
            return;
        }
        if (TextUtils.isEmpty(contactCity)) {
            mView.showError("城市不能为空！");
            return;
        }
        if (TextUtils.isEmpty(contactAddress)) {
            mView.showError("地址不能为空！");
            return;
        }
//        if (TextUtils.isEmpty(businessLicense)) {
//            mView.showError("请上传营业执照！");
//            return;
//        }

        mModel.organizationCertification(organizationName, organizationFullName, contactName,
                contactPosition, contactMobile, contactEmail,
                contactCity, contactAddress, businessLicense)
                .compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.dismissLoading();
                mView.showAuthSuccess();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });


    }
}
